package com.base.cn.platform.os.config;

import com.base.cn.platform.os.common.interceptor.BaseDataInterceptor;
import com.base.cn.platform.os.common.interceptor.keyword.KeyWordFilter;
import com.base.cn.platform.os.common.xss.XssCodeFilter;
import com.base.cn.platform.os.interceptor.RequestTokenInterceptor;
import com.base.cn.platform.os.interceptor.WebAccessJumpInterceptor;
import com.base.cn.platform.os.interceptor.WebUserLoginInterceptor;
import com.base.cn.platform.os.service.manage.op.keyword.OpKeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * 前台配置
 *
 * @author s.li
 * @create 2018-05-11-9:41
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private OpKeywordService opKeywordService;
    @Autowired
    private RequestTokenInterceptor requestTokenInterceptor;
    //敏感词、非字过滤路径
    private static  List<String> urlList = new ArrayList<>();
    {
    urlList.add("/web/uc/ajax/saveUserPersonalData");//保存用户信息
    urlList.add("/web/uc/course/note/saveCourseNote");//保存课程笔记
    urlList.add("/web/uc/course/comment/saveCourseComment");//保存课程评论
    urlList.add("/web/uc/qa/saveQuestion");//保存问答
    urlList.add("/web/uc/qa/reply/saveQaReply");//提交问答回复
    urlList.add("/web/uc/qa/add/saveQuestionAdd");//提交问答追问
    urlList.add("/web/classes/topic/saveTopic");//发表话题
    urlList.add("/web/classes/topic/saveTopicReply");//发表话题回复
    urlList.add("/web/classes/topic/saveTopicReport");//提交话题举报
    urlList.add("/web/uc/exam/saveExamQuestionCorrect");//试题纠错
    urlList.add("/web/feedback/saveUpdateFeedBack");//用户反馈
    urlList.add("/web/uc/exam/saveQuestionRecord");//保存试题作答记录
    }

    /**
     * 前台用户登录拦截器
     * @return
     */
    @Bean
    public WebUserLoginInterceptor webUserLoginInterceptor(){
        return new WebUserLoginInterceptor();
    }

    /**
     * 访问跳转拦截
     * @return WebAccessJumpInterceptor
     */
    @Bean
    public WebAccessJumpInterceptor webAccessJumpInterceptor(){
        return new WebAccessJumpInterceptor();
    }

    /**
     * 内容过滤器
     * @return
     */
    @Bean
    public BaseDataInterceptor baseDataInterceptor(){
        return new BaseDataInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //registry.addInterceptor(requestTokenInterceptor).addPathPatterns("/web/**").excludePathPatterns("/web/webError");
        registry.addInterceptor(webAccessJumpInterceptor()).addPathPatterns("/web/**").excludePathPatterns("/web/account/weiXinRedirect","/web/account/getWeiXinCode");
        registry.addInterceptor(baseDataInterceptor()).addPathPatterns("/web/**");
        registry.addInterceptor(webUserLoginInterceptor()).addPathPatterns("/web/uc/**");
    }

    @Bean
    public FilterRegistrationBean xssCodeFilter(){
        FilterRegistrationBean xssCodeFilter  = new FilterRegistrationBean();
        xssCodeFilter.setFilter(new XssCodeFilter());
        xssCodeFilter.setName("xssCodeFilter");
        xssCodeFilter.setOrder(2);
        xssCodeFilter.setUrlPatterns(urlList);
        return xssCodeFilter;
    }

    /**
     * 敏感词过滤
     * @return FilterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean keyWordFilter(){
        FilterRegistrationBean keyWordFilter = new FilterRegistrationBean();
        keyWordFilter.setFilter(new KeyWordFilter(opKeywordService));
        keyWordFilter.setName("keyWordFilter");
        keyWordFilter.setUrlPatterns(urlList);
        keyWordFilter.setOrder(1);
        return keyWordFilter;
    }
}
